﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.Entity;
using PBOffice.DAL;
using PBOffice.BLL;

public partial class PersonnelManagementPage_addEmployee : System.Web.UI.Page
{
	UserManager um = new UserManager();
	User user = new User();
	TreeViewManager tv = new TreeViewManager();
	protected void Page_Load(object sender, EventArgs e)
	{
		txtDepId.ValueChanged += tvDepId_SelectedNodeChanged;
		//页面首次加载
		if (!IsPostBack)
		{
			ShowDiploma();                           //加载学历信息
			//修改员工信息
			if (Request.QueryString.ToString().Contains("UserNo"))
			{
				int userNo = Convert.ToInt32(Request.QueryString["UserNo"]);         //获取通过URL传递过来的员用户编号
				v_UserDepPosition user = um.ShowUserInfoByUserNo(userNo);            //根据用户编号获取用户信息
				txtUserName.Text = user.UserName;      //用户名
				txtUserNo.Text = userNo.ToString();    //用户编号
				if (user.UserGender == true)
					rbtnlGender.SelectedValue = "True";      //如果是男性，则性别选中男
				else
					rbtnlGender.SelectedValue = "False";     //如果是女性，则性别选中女
				ViewState["selectedText"] = user.DepId;                  //部门名称
				ViewState["selectedValue"] = user.DepNo;               //部门编号
				ddlPositionId.SelectedItem.Text = user.PosId; //职位名称
				if (user.UserLeaderNo == 0)                //直接上级
					ddlLeader.Text = "无上级";
				else
					ddlLeader.SelectedValue = user.UserLeaderNo.ToString();   //直接上级
				ddlDergee.SelectedValue = user.UserDiplomaNo.ToString();      //学历

				txtEntryTime.Text = user.UserEntryTime.ToString();    //入职日期
				txtBirthday.Text = user.UserBirthday.ToString();      //出生日期
				txtExperience.Text = user.UserCareer;                  //工作履历
				txtLandLine.Text = user.UserLandline;      //座机
				txtMobile.Text = user.UserMobile;          //手机
				txtAddr.Text = user.UserAddr;              //地址
				txtPostCode.Text = user.UserPostcode;      //邮编
				txtEmail.Text = user.Email;                //Email
				imgUser.ImageUrl = user.UserHeadimg;       //头像
			}
			//添加新员工
			else
			{
				//int depNo;
				//ShowPosition(depNo);                   //加载岗位信息
			}
		}
	}
	//加载学历信息
	private void ShowDiploma()
	{
		List<UserDiploma> dipList = new DiplomaManager().ShowAllDiplomaInfo();  //查询学历信息
		ddlDergee.DataSource = dipList;                                         //数据绑定
		ddlDergee.DataTextField = "UserDiplomaId";
		ddlDergee.DataValueField = "UserDiplomaNo";
		ddlDergee.DataBind();
	}
	//显示领导信息
	private void ShowLeader(string userName)
	{
		List<v_UserDepPosition> userList = um.ShowUserInfoByName(userName);   //根据姓名模糊查询员工信息
		ddlLeader.DataSource = userList;                 //为直接上级控件绑定数据信息
		v_UserDepPosition item = new v_UserDepPosition();
		item.UserName = "无上级";
		item.UserNo = 0;
		userList.Insert(0, item);                          //为集合添加一个新项
		ddlLeader.DataTextField = "UserName";            //显示字段：员工姓名
		ddlLeader.DataValueField = "UserNo";             //隐藏字段：员工编号
		ddlLeader.DataBind();
	}
	//选择部门节点变化事件
	protected void tvDepId_SelectedNodeChanged(object sender, EventArgs e)
	{
		int depNo = Convert.ToInt32(txtDepId.SelectedValue);       //部门编号
		ShowPosition(depNo);                                                      //根据部门编号加载职位信息
	}
	//显示职位信息
	private void ShowPosition(int depNo)
	{
		List<v_DepPosition> posList = new PositionManager().ShowPosInfoByDepNo(depNo);   //根据部门编号获取职位信息列表
		ddlPositionId.DataSource = posList;
		ddlPositionId.DataTextField = "PosId";          //显示字段：职位名称
		ddlPositionId.DataValueField = "PosNo";         //隐藏字段：职位编号
		ddlPositionId.DataBind();
	}

	/// <summary>
	/// 提交按钮单击事件
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnOk_Click(object sender, EventArgs e)
	{
		int depNo;
		int posNo;
		int dipNo;
		GetUserInfo(out depNo, out posNo, out dipNo);   //获取用户信息
		if (Request.QueryString.ToString().Contains("UserNo"))
		{
			//修改员工信息
			um.ChangeUserInfo(user, depNo, posNo, dipNo);
		}
		else
		{
			//添加员工信息
			um.AddUserInfo(user, depNo, posNo, dipNo);
			UploadHeadImg();    //上传头像
		}
	}
	//上传头像
	private void UploadHeadImg()
	{
		if (fulImage.HasFile)
		{
			string imgFileName = fulImage.FileName;    //头像图片名
			string imgRelativePath = user.UserHeadimg;   //头像在服务器端相对路劲
			string imgFullPath = Server.MapPath(imgRelativePath);              //头像在服务器端的绝对路劲
			FileInfo file = new FileInfo(imgFileName);      //实例化对象
			string imgExtension = file.Extension.ToLower();     //获取扩展名并转换为小写
			if (imgExtension == ".jpg" || imgExtension == ".jpeg" || imgExtension == ".gif" || imgExtension == ".png" || imgExtension == ".bmp")      //判断上传文件的格式
			{
				fulImage.SaveAs(imgFullPath);        //将头像保存到服务器的指定路径
			}
			else     //格式错误时，给出提示信息
			{
				Page.ClientScript.RegisterClientScriptBlock(typeof(string), "Error", "alert('请选择格式为：.gif .jpg .jpeg .bmp .png的图片')", true);            //提示信息
			}
		}
		else
		{
			user.UserHeadimg = "..Images/Staffs/noperson.jpg";    //没有上传头像时，使用默认头像
		}
	}
	/// <summary>
	/// 获取用户信息
	/// </summary>
	/// <param name="depNo"></param>
	/// <param name="posNo"></param>
	/// <param name="dipNo"></param>
	private void GetUserInfo(out int depNo, out int posNo, out int dipNo)
	{
		user.UserName = txtUserName.Text.Trim();    //姓名
		user.UserNo = Convert.ToInt32(txtUserNo.Text.Trim());   //用户编号
		if (rbtnlGender.SelectedIndex == 0)
			user.UserGender = true;        //男
		else
			user.UserGender = false;       //女
		depNo = Convert.ToInt32(txtDepId.SelectedValue.ToString());  //部门编号
		posNo = Convert.ToInt32(ddlPositionId.SelectedValue.ToString());  //职位编号
		user.UserLeaderNo = Convert.ToInt32(ddlLeader.SelectedValue.ToString());  //直接上级编号
		dipNo = Convert.ToInt32(ddlDergee.SelectedValue.ToString());      //学历编号
		user.UserEntryTime = Convert.ToDateTime(txtEntryTime.Text.Trim());    //用户入职日期
		user.UserBirthday = Convert.ToDateTime(txtBirthday.Text.Trim());      //出生日期
		user.UserCareer = txtExperience.Text.Trim();   //工作经历
		user.UserLandline = txtLandLine.Text.Trim();   //座机
		user.UserMobile = txtMobile.Text.Trim();       //手机
		user.UserAddr = txtAddr.Text.Trim();           //联系地址
		user.UserPostcode = txtPostCode.Text.Trim();   //邮编
		user.Email = txtEmail.Text.Trim();             //Email         
		user.UserHeadimg = "..Images/Staffs/" + fulImage.FileName;       //头像在服务器端地址
	}
	/// <summary>
	/// 取消按钮单击事件
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancel_Click(object sender, EventArgs e)
	{
		Response.Close();   //  关闭页面 
	}

}